<template>
  <div class="info" v-loading="loading">
    <div class="card_con search_data mb15">
      <el-row class="mb15">
        <el-col :span="8" class="pr40">
          <div class="fz13 mb10">关键搜索：</div>
          <el-input
            clearable
            v-model="keyName"
            placeholder="请输入商品id/商品名/店铺名"
          ></el-input>
        </el-col>
        <el-col :span="8" class="pr40">
          <div class="fz13 mb10">时间</div>
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="time"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-col>
        <el-col :span="8" class="pr40">
          <div class="fz13 mb10">商品分类：</div>
          <!-- <el-select v-model="typeId" placeholder="请选择">
            <el-option
              v-for="item in typeArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select> -->
          <el-cascader
            clearable
            @change="cascaderChange"
            :props="optionProps"
            :options="typeArr"
            v-model="typeId"
          >
            <template slot-scope="{ data }">
              <span>{{ data.name }}</span>
            </template>
          </el-cascader>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" class="pr40">
          <div class="fz13 mb10">状态：</div>
          <el-select clearable v-model="shopStateId" placeholder="请选择">
            <el-option
              v-for="item in shopStateArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="8" class="pr40">
          <div class="fz13 mb10">商品类型：</div>
          <el-select clearable v-model="shopLineStateId" placeholder="请选择">
            <el-option
              v-for="item in shopLineStateArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <div class="fz13 mb10">&nbsp;</div>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="card_con">
      <div class="row mb15">
        <span class="l" @click="handleAdd">
          <i class="el-icon-circle-plus-outline"></i>添加商品
        </span>
        <span class="l l2" @click="handleAllDelete">
          <i class="el-icon-delete"></i>批量删除
        </span>
      </div>
      <el-table
        border
        :data="tableData"
        :header-cell-style="{
          'text-align': 'center',
        }"
        :cell-style="{ 'text-align': 'center' }"
        style="width: 100%"
        highlight-current-row
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" min-width="5%"> </el-table-column>
        <el-table-column prop="id" label="id" min-width="10%">
        </el-table-column>
        <el-table-column
          :show-overflow-tooltip="true"
          prop="name"
          label="商品名"
          min-width="20%"
        >
        </el-table-column>
        <el-table-column prop="store.name" label="店铺名" min-width="10%">
        </el-table-column>
        <el-table-column
          prop="product_cate_text"
          label="商品分类"
          min-width="10%"
        >
        </el-table-column>
        <el-table-column label="商品图" min-width="10%">
          <template slot-scope="scope">
            <div class="slt">
              <el-popover placement="right" width="200" trigger="hover">
                <img class="tbImg1" :src="scope.row.thumb.src" alt="" />
                <img
                  class="tbImg"
                  slot="reference"
                  :src="scope.row.thumb.src"
                  alt=""
                />
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="现价" min-width="10%">
        </el-table-column>
        <el-table-column prop="sale" label="销量" min-width="10%">
        </el-table-column>
        <el-table-column prop="is_online" label="商品类型" min-width="10%">
          <template slot-scope="scope">
            <div v-if="scope.row.is_online === 1">线上</div>
            <div v-if="scope.row.is_online === 0">线下</div>
            <div v-if="scope.row.is_online === 2">线上线下</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" min-width="10%">
          <template slot-scope="scope">
            <el-switch
              @change="changeSwitch(scope.row)"
              class="tablescope"
              v-model="scope.row.status"
              active-text="下架"
              inactive-text="上架"
              :active-value="1"
              :inactive-value="2"
              inactive-color=""
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" min-width="20%">
        </el-table-column>
        <el-table-column label="操作" min-width="20%">
          <template slot-scope="scope">
            <div class="caozuo">
              <div
                class="handleExamine hand_cs"
                @click="handleDetail(scope.row)"
              >
                详情
              </div>
              <div class="handleEdit hand_cs" @click="handleEdit(scope.row)">
                编辑
              </div>
              <div
                class="handleDelete red hand_cs"
                @click="handleDelete(scope.row)"
              >
                删除
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination :page="page" @onCurrentChange="handleCurrentChange" />
      </div>
      <!-- 编辑 添加 -->
      <el-dialog
        class="editDialog"
        :title="title"
        :visible.sync="dialogVisible"
        width="800px"
        :close-on-click-modal="false"
      >
        <Dialog
          v-if="dialogVisible"
          :rowData="rowData"
          @handleClose="handleClose"
        ></Dialog>
      </el-dialog>
      <!-- 详情 -->
      <el-dialog
        class="editDialog"
        title="详情"
        :visible.sync="detailDialog"
        width="70%"
      >
        <DetailDialog v-if="detailDialog" :rowId="rowId"></DetailDialog>
      </el-dialog>
      <!-- 上架下架开关 -->
      <el-dialog
        title="上下架备注"
        :visible.sync="up_down_dialog"
        :close-on-click-modal="false"
        @close="remarkClose"
        width="500px"
      >
        <div class="beizhu">
          <div class="bz">
            <el-input v-model="remarks" placeholder="请输入备注"></el-input>
          </div>
          <div class="btn">
            <el-button type="primary" @click="handleRemark">提交</el-button>
          </div>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Dialog from "./shop_list_edit.vue";
import DetailDialog from "./shop_detail_dialog.vue";
import Pagination from "@/components/Pagination.vue";
import { allShopList, shopCategory, switchRemark } from "@/api/mall_management";
export default {
  components: { Pagination, Dialog, DetailDialog },
  data() {
    return {
      loading: true,
      keyName: "",
      time: "",
      tableData: [],
      selectionKeys: [], //表格选择数据
      typeArr: [],
      typeId: [],
      optionProps: {
        value: "id",
        label: "name",
        children: "children",
        checkStrictly: true,
      },
      shopStateArr: [
        { id: "1", name: "上架" },
        { id: "2", name: "下架" },
      ],
      shopStateId: "",
      shopLineStateArr: [
        { id: "1", name: "线上" },
        { id: "0", name: "线下" },
        { id: "2", name: "线上线下" },
      ],
      shopLineStateId: "",
      page: {
        total: 0,
        pageNum: 1, // 当前页数
        pageSize: 10, // 每页的条数
      },
      remarks: "", //开关备注输入
      remarks_state: "", //开关参数
      dialogVisible: false,
      title: "",
      detailDialog: false,
      up_down_dialog: false,
      rowId: "", //当前行id
      rowData: "",
    };
  },
  methods: {
    //商品列表
    async allShopList() {
      const { data, err } = await allShopList({
        page: this.page.pageNum,
        key: this.keyName,
        st_s: this.time ? this.time[0].valueOf() : 0,
        st_e: this.time ? this.time[1].valueOf() : 0,
        status: this.shopStateId,
        is_online: this.shopLineStateId,
        type_id: this.typeId.join(","),
        type: 1,
      });
      if (err === 0) {
        this.loading = false;
        //正确
        this.tableData = data.data;
        this.page.total = data.total;
        this.page.pageNum = data.current_page;
        this.page.pageSize = data.per_page;
      }
    },
    //商品类型
    async shopCategory() {
      const { data, err } = await shopCategory({});
      if (err === 0) {
        this.typeArr = data;
        this.allShopList();
      }
    },
    //开关备注 删除接口
    async switchRemark() {
      const { data, err } = await switchRemark({
        remark: this.remarks,
        id: this.rowId,
        type: this.remarks_state,
      });
      if (err === 0) {
        this.up_down_dialog = false;
        this.remarks = "";
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
        if (this.remarks_state === "3") {
          this.allShopList();
        }
      }
    },
    //查询
    handleSearch() {
      this.loading = true;
      this.page.pageNum = 1;
      this.allShopList();
    },
    //新增
    handleAdd() {
      this.rowData = {};
      this.dialogVisible = true;
      this.title = "新增";
    },
    //编辑
    handleEdit(row) {
      this.rowData = row;
      this.dialogVisible = true;
      this.title = "编辑";
    },
    //详情
    handleDetail(row) {
      this.rowId = row;
      this.detailDialog = true;
    },
    //级联
    cascaderChange() {
      console.log(this.typeId);
    },
    //删除
    handleDelete(row) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.remarks_state = "3";
          this.rowId = row.id;
          this.switchRemark();
          //   this.$message({
          //     type: "success",
          //     message: "删除成功!",
          //     showClose: true,
          //   });
        })
        .catch(() => {
          console.log("1111111111111");
        });
    },
    //批量删除
    handleAllDelete() {
      if (this.selectionKeys.length === 0) {
        this.$message({
          showClose: true,
          message: "请选择要操作的数据",
          type: "warning",
        });
      } else {
        this.$confirm("是否确认删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.remarks_state = "3";
            this.switchRemark();
            //   this.$message({
            //     type: "success",
            //     message: "删除成功!",
            //     showClose: true,
            //   });
          })
          .catch(() => {});

        //掉接口
      }
    },
    //表格选择数据
    handleSelectionChange(rows) {
      this.selectionKeys = rows.map((item) => item.id);
      this.rowId = this.selectionKeys.join(",");
    },
    // 当前页: ${val}
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.loading = true;
      this.allShopList();
    },
    //关闭添加编辑
    handleClose() {
      this.dialogVisible = false;
      this.allShopList();
    },
    //开关编辑
    changeSwitch(row) {
      //0上架 1   1下架 2
      this.up_down_dialog = true;
      this.rowId = row.id;
      console.log(row.status);
      this.remarks_state = row.status;
      //   if (row.status === 1) {
      //     //1上架 操作下架
      //     this.remarks_state = 2;
      //   } else {
      //     //2下架  操作上架
      //     this.remarks_state = 1;
      //   }
      //
    },
    //开关备注提交
    handleRemark() {
      if (this.remarks === "") {
        this.$message({
          showClose: true,
          message: "请输入备注",
          type: "warning",
        });
      } else {
        this.up_down_dialog = false;
        this.switchRemark();
      }
    },
    //开关弹窗关闭
    remarkClose() {
      this.up_down_dialog = false;
      this.remarks = "";
      this.allShopList();
    },
  },
  created() {
    this.shopCategory();
  },
};
</script>

<style lang="scss" scoped>
@import "@/style/index.scss";

.info {
  .title {
    font-size: 16px;
    margin-bottom: 13px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  padding-top: 10px;
}
.el-select {
  width: 100%;
}

.tb {
  background-color: #fff;
  padding: 20px;
  box-sizing: border-box;
}
.row {
  display: flex;
  border-radius: 2px;
  //   border: 1px solid #ccc;
  .l {
    padding: 8px 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #ccc;
    font-size: 13px;
    i {
      margin-right: 6px;
      font-size: 13px;
    }
    &:hover {
      cursor: pointer;
    }
  }
  .l:nth-child(1) {
    border-right: 0;
  }
}
.editDialog {
  //   z-index: 2005 !important;
  /deep/.el-dialog {
    height: 75%;
    .el-dialog__body {
      height: 80%;
      overflow: auto;
    }
  }
}
.handleEdit {
  margin-left: 20px;
}
.handleDelete {
  margin-left: 20px;
}

/deep/.el-table .el-table__cell {
  padding: 10px 0;
}
.search_data {
  /deep/.el-input__inner {
    height: 36px;
    line-height: 36px;
    font-size: 13px;
  }
}

.beizhu {
  padding: 0 20px 50px 20px;
  box-sizing: border-box;
  .bz {
    margin-bottom: 10px;
    margin-top: 20px;
  }
  .btn {
    margin-top: 20px;
    .el-button {
      width: 100%;
    }
  }
}
</style>
